﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DTC.Database;
using DTC.Popups;

namespace DTC.Pages
{
    /// <summary>
    /// Interaction logic for PatiensListPage.xaml
    /// </summary>
    public partial class PatiensListPage : Page
    {
        public PatiensListPage()
        {
            InitializeComponent();
            patients = new ObservableCollection<DTCPatient>(App.DBI.GetAllPatiens());
            PatientsGrid.ItemsSource = patients;
            UsualSort();
        }

        private ICollectionView view;

        private ObservableCollection<DTCPatient> patients;

        public void AddPatient(DTCPatient newPatient)
        {
            patients.Add(newPatient);
        }

        private void DoctorGroupCheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (PatientsGrid.ItemsSource != null)
            {
                if (view.GroupDescriptions.Count != 0)
                    view.GroupDescriptions.RemoveAt(0);
                view.GroupDescriptions.Add(new PropertyGroupDescription("DTCDoctor.FullName"));
            }
        }

        private void DiseaseGroupRadioButton_Click(object sender, RoutedEventArgs e)
        {
            if (PatientsGrid.ItemsSource != null)
            {
                if (view.GroupDescriptions.Count != 0)
                    view.GroupDescriptions.RemoveAt(0);
                view.GroupDescriptions.Add(new PropertyGroupDescription("DTCDisease.Code"));
            }
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            if (PatientsGrid.ItemsSource != null)
            {
                string[] tmpStrSet = SearchStringTextBox.Text.Trim().Split(new char[] { ' ' });
                string tmpstr;
                SearchString = new Collection<string>();
                foreach (string str in tmpStrSet)
                {
                    tmpstr = str.Trim();
                    if (str.Length != 0)
                        SearchString.Add(tmpstr);
                }
                var collectionView = (ListCollectionView)
                    CollectionViewSource.GetDefaultView(PatientsGrid.ItemsSource);
                collectionView.Filter = new Predicate<object>(FilterRecs);
            }

        }
        Collection<string> SearchString;
        private bool FilterRecs(Object item)
        {
            //search done here
            if (SearchString.Count == 0)
                return true;

            if (item is DTCPatient)
            {
                bool matches = true;
                var pat = item as DTCPatient;
                foreach (string substring in SearchString)
                {
                    bool foundsub = false;
                    if (substring == pat.PatientNumber)
                        foundsub = true;
                    else if (substring == pat.Surname)
                        foundsub = true;
                    else if (substring == pat.Name)
                        foundsub = true;
                    else if (substring == pat.Fathername)
                        foundsub = true;
                    else if (pat.DTCDisease != null)
                    {
                        if (substring == pat.DTCDisease.Code)
                            foundsub = true;
                        else if (substring == pat.DTCDisease.Name)
                            foundsub = true;
                    }
                    if (!foundsub)
                    {
                        matches = false;
                        break;
                    }
                }
                return matches;

            }
            return false;
        }

        private void NoGoupingRadioButton_Click(object sender, RoutedEventArgs e)
        {
            if (view.GroupDescriptions.Count != 0)
                view.GroupDescriptions.RemoveAt(0);
        }

        private void UsualSort()
        {
            if (PatientsGrid.ItemsSource != null)
            {
                view = CollectionViewSource.GetDefaultView(PatientsGrid.ItemsSource);
                view.SortDescriptions.Add(
                  new SortDescription("PatientNumber", ListSortDirection.Ascending));
                view.SortDescriptions.Add(
                  new SortDescription("DTCDisease.Code", ListSortDirection.Ascending));
                view.SortDescriptions.Add(
                  new SortDescription("Surname", ListSortDirection.Ascending));
                view.SortDescriptions.Add(
                  new SortDescription("Name", ListSortDirection.Ascending));
                view.SortDescriptions.Add(
                  new SortDescription("Fathername", ListSortDirection.Ascending));
            }
        }

        private void AddRecord_Click(object sender, RoutedEventArgs e)
        {
            var mrp = new MedicalRecordPopup {Owner = Application.Current.MainWindow};
            if (mrp.ShowDialog() == true)
            {
                patients.Add(mrp.Patient);
            }
        }
    }
}
